package com.sj.purchase.modules.cost.web;

import com.sj.purchase.common.Result;
import com.sj.purchase.modules.cost.service.stock.StockReportService;
import com.sj.purchase.modules.cost.views.stock.*;
import com.sj.purchase.util.ExcelDownloadUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.function.Function;

import static com.sj.purchase.common.ResultGenerator.ok;

/**
 * 库存报表
 * Created by yangrd on 2017/7/19.
 */
@RestController
@RequestMapping("/api/stock/report")
public class StockReportController {

    private static final String DATE_FORMAT = "yyyy-MM-dd";
    private static Function<Date, String> toDay = (date -> DateFormatUtils.format(date, DATE_FORMAT));

    @Autowired
    private StockReportService service;

    /**
     * 入库 与订单有关
     */
    @GetMapping("in-stock")
    public Result<Page<InStockVO>> inStock(Date day, @RequestParam(defaultValue = "") String sku, Pageable pageable) {
        Pair<List<InStockVO>, Long> pair = service.getInStockContentAndTotal(day, sku, pageable);
        return ok(new PageImpl<>(pair.getLeft(), pageable, pair.getRight()));
    }

    @GetMapping("in-stock/excel")
    public void exportInStockExcel(Date day, @RequestParam(defaultValue = "") String sku) {
        download(toDay.apply(day) + "入库", InStockVO.class, service.getInStockContentAndTotal(day, sku, null).getLeft());
    }

    /**
     * 出库 ---> 查询当天的sku -> sku去查仓库 sku数量 (与订单无关)
     */
    @GetMapping("out-stock")
    public Result<Page<OutStockVO>> outStock(Date day, @RequestParam(defaultValue = "") String sku, Pageable pageable) {
        Pair<List<OutStockVO>, Long> pair = service.getOutStockContentAndTotal(day, sku, pageable);
        return ok(new PageImpl<>(pair.getLeft(), pageable, pair.getRight()));
    }

    @GetMapping("out-stock/excel")
    public void exportOutStockExcel(Date day, @RequestParam(defaultValue = "") String sku) {
        download(toDay.apply(day) + "出库", OutStockVO.class, service.getOutStockContentAndTotal(day, sku, null).getLeft());
    }

    /**
     * 期初 ---> 查询当天的sku -> sku去查仓库 sku数量
     */
    @GetMapping("begin-stock")
    public Result<Page<BeginStockVO>> beginStock(Date day, @RequestParam(defaultValue = "") String sku, Pageable pageable) {
        return ok(service.beginStockPage(day, sku, pageable));
    }

    @GetMapping("begin-stock/excel")
    public void exportBeginStockExcel(Date day, @RequestParam(defaultValue = "") String sku) {
        download(toDay.apply(day) + "期初", BeginStockVO.class, service.getBeginStockContentAndTotal(day, sku, null).getLeft());
    }

    /**
     * 期末 ---> 期初+入库-出库
     */
    @GetMapping("end-stock")
    public Result<Page<EndStockVO>> endStock(Date day, @RequestParam(defaultValue = "") String sku, Pageable pageable) {
        Pair<List<EndStockVO>, Long> pair = service.getEndStockContentAndTotal(day, sku, pageable);
        return ok(new PageImpl<>(pair.getLeft(), pageable, pair.getRight()));
    }

    @GetMapping("end-stock/excel")
    public void exportEndStockExcel(Date day, @RequestParam(defaultValue = "") String sku) {
        download(toDay.apply(day) + "期末", EndStockVO.class, service.getEndStockContentAndTotal(day, sku, null).getLeft());
    }

    private <T extends IStockVO<T>> void download(String name, Class<T> t, List<T> content) {
        ExcelDownloadUtil.easyPoi(name, t, content);
    }
}